<!DOCTYPE html>
<html>
<head>
<include file="Include:meta" />
<title>添加项目款项-目的地运营集团数据管理平台</title>
<include file="Include:style" />
<style>
	.w50{ width: 50px; }
</style>
</head>
<body>
<include file="Include:header" />
<include file="Include:left" />
	<div id="main">
		<div class="crumbs">
			<div class="crumbs-text">项目款项列表 > 添加项目款项</div>
		</div>
		<div id="content">
			<form action="__APP__/Financial/project_funds_add" method="post" id="project_add_form">
				<input type="hidden" name="action" value="add">
				<input type="hidden" name="mode" value="{$mode}">
				<div class="form-main">
					<div class="form-title">添加项目款项</div>
					<div class="form-item">
						<div class="input-label"><span class="asterisk">*</span>所属中心</div>
						<div class="input-item">
							<select name="center" class="form-control w200 required_select" autocomplete="off">
								<foreach name="pro_list" item="vo">
								<option value="{$vo['name2']}">{$vo['name1']}</option>
								</foreach>
							</select>
							<span class="error-item">必填项</span>
						</div>
					</div>
					<div class="form-item">
						<div class="input-label"><span class="asterisk">*</span>项目编号/名称</div>
						<div class="input-item">
							<select name="pid" class="form-control w400 required_select" autocomplete="off">
								<option value="-10">请选择</option>
							</select>
							<span class="error-item">必填项</span>
						</div>
					</div>
					<div class="form-item">
						<div class="input-label">合同金额</div>
						<div class="input-item">
							<input type="text" class="form-control w100" name="contract_money" autocomplete="off">
							<span class="error-item">必填项</span>
							<span class="error-item">必须为正整数</span>
						</div>
					</div>
					<div class="form-item">
						<div class="input-label"><span class="asterisk">*</span>合同日期</div>
						<div class="input-item">
							<input type="text" name="contract_date" class="form-control Wdate w100 required_text" onClick="WdatePicker()" readonly autocomplete="off">
							<span class="error-item">必填项</span>
						</div>
					</div>
					
					<div class="form-item">
						<div class="input-label">合同附件</div>
						<div class="input-item">
							<div class="input-group w250 left">
								<input type="text" name="file_name" class="form-control file-name" placeholder="请上传附件" readonly autocomplete="off">
								<span class="input-group-btn">
									<button class="btn file-submit" type="button">上传</button>
								</span>
							</div>
							<button class="btn file-delete ml5 hide" type="button">删除</button>
							<input type="hidden" name="file_url" class="file-url" autocomplete="off">
							<span class="error-item">请上传文件</span>
							<span class="error-item">文件名必填</span>
						</div>
					</div>
					<div class="form-item">
						<div class="input-label"><span class="asterisk">*</span>款项阶段</div>
						<div class="input-item">
							<select name="stages" class="form-control w100 required_select" autocomplete="off">
								<for start="1" end="20" comparison="elt" step="1">
								<option value="{$i}">{$i}</option>
								</for>
							</select>
							<span class="error-item">必填项</span>
						</div>
					</div>
					<div class="form-sub-title">款项阶段</div>
					<div class="form-table-item">
						<table class="table stage_table">
							<tr class="form-table-header">
								<td>阶段</td>
								<td><span class="asterisk">*</span>应收款金额</td>
								<td>百分比</td>
								<td><span class="asterisk">*</span>应收款日期</td>
								<td><span class="asterisk">*</span>应开票金额</td>
								<td><span class="asterisk">*</span>应开票日期</td>
								<td><span class="asterisk">*</span>预警日期</td>
								<td><span class="asterisk">*</span>催款日期</td>
							</tr>
							<for start="1" end="20" comparison="elt" step="1">
								<tr <if condition="$i eq 1">class="cur tr"<else />class="hide tr"</if>>
									<td>└第{$i}阶段</td>
									<td>
										<input type="text" name="stage_money[]" class="form-control td_text w100 money_in" autocomplete="off">
										<span class="error-item">必填项</span><span class="error-item">必须为大于0的正数</span>
									</td>
									<td>
										<span class="bili"></span>
										<input type="hidden" name="bili[]" value="">
										<input type="hidden" name="stage[]" value="{$i}">
									</td>
									<td>
										<input type="text" name="stage_date[]" class="form-control Wdate w100 td_text" onClick="WdatePicker()" readonly autocomplete="off">
										<span class="error-item">必填项</span>
									</td>
									<td>
										<input type="text" name="ticket_money[]" class="form-control td_text w100 money_all" autocomplete="off">
										<span class="error-item">必填项</span><span class="error-item">必须为大于0的正数</span>
									</td>
									<td>
										<input type="text" name="ticket_date[]" class="form-control Wdate w100 td_text" onClick="WdatePicker({onpicked:dd})" readonly autocomplete="off">
										<span class="error-item">必填项</span>
									</td>
									<td>
										<input type="text" name="warn_date[]" class="form-control Wdate w100 td_text" onClick="WdatePicker()" readonly autocomplete="off">
										<span class="error-item">必填项</span>
									</td>
									<td>
										<input type="text" name="press_date[]" class="form-control Wdate w100 td_text" onClick="WdatePicker()" readonly autocomplete="off">
										<span class="error-item">必填项</span>
									</td>
								</tr>
							</for>
							<tr>
								<td>合计</td><td class="sk_all"></td><td class="bili_all"></td><td></td><td class="kp_all"></td><td></td><td></td><td></td>
							</tr>
						</table>
					</div>
				</div>
				<p class="red Text_c bili_item hide"></p>
				<div class="form-btn-item"><input type="submit" value="提交" class="btn btn-submit"><button class="btn btn-default goback" type="button">返回</button></div>
			</form>
		</div>
	</div>
</body>
<include file="Include:javascript" />
<script type="text/javascript">
	$(function() {
		KindEditor.ready(function(K){
			//上传附件
			var editorFile = K.editor();
			$('.file-submit').on('click',function(){
				var parents = $(this).parents(".input-group");
				editorFile.loadPlugin('insertfile',function(){
					document.cookie="file_name=project_funds";
					document.cookie="file_type=ppt,pptx,pdf,doc,docx,jpg,jpeg,png,xls,xlsx,txt,rar,zip";
					editorFile.plugin.fileDialog({
						clickFn:function(url,title){
							parents.nextAll(".error-item").hide();
							parents.find(".file-name").val(title).prop("readonly",false);
							parents.nextAll(".file-url").val(url);
							parents.nextAll(".file-delete").show();
							editorFile.hideDialog();
						}
					});
				});
			});
		});
		
		//删除附件
		$(".file-delete").click(function(){
			$(this).hide();
			$(this).nextAll(".error-item").hide();
			$(this).nextAll(".file-url").val("");
			$(this).prevAll(".input-group").find(".file-name").val("").prop("readonly",true);
		});
		/*阶段联动*/
		$("[name='stages']").change(function(){
			var val = $(this).val();
			$('.stage_table tr').each(function(i){
				if(val>i){
					$('.stage_table .tr').eq(i).show().addClass('cur');
				}else{
					$('.stage_table .tr').eq(i).hide().removeClass('cur').find('.form-control').val('');
				}
			})
			var sk = 0;
			$(".cur .money_in").each(function(){
				var val = $(this).val();
				sk+= +val;
			})
			if(sk=='0'){
				$('.sk_all').html('');
			}else{
				$('.sk_all').html(sk);
			}
			var kp = 0;
			$(".cur .money_all").each(function(){
				var val = $(this).val();
				kp+= +val;
			})
			if(kp=='0'){
				$('.kp_all').html('');
			}else{
				$('.kp_all').html(kp);
			}
			var sk = $('.sk_all').html(),
				all = $("[name='contract_money']").val(),
				ts = sk/all*100,
				last = ts.toFixed(2)-0;
			if(all!=''){
				if(sk){
					$('.bili_all').html(""+last+"%");
				}
			}
		})
		$("[name='center']").change(function(){
			var val = $(this).val();
			$.ajax({
				type:"POST",
				async:false,
				dataType:"json",
				data:{"center_id":val,"action":"project"},
				url:"__APP__/Financial/project_funds_ajax",
				success:function(data){
					if(data){
						var html = "<option value='-10'>请选择</option>";
						for(var i=0;i<data.length;i++){
							html+= "<option value='"+data[i].id+"'>["+data[i].project_num+"]"+data[i].project_name+"</option>"
						}
						$("[name='pid']").html(html).select2();
					}
				}
			})
		})
		/*百分比换算*/
		$('body').on('blur','.cur .money_in',function(){
			var val = $(this).val(),
				all = $("[name='contract_money']").val(),
				end = val/all*100,
				end2 = end.toFixed(2)-0;
			$(this).parents('tr').find(".money_all").val(val);
			var sk = 0;
			$(".cur .money_in").each(function(){
				var val = $(this).val();
				sk+= +val;
			})
			if(sk=='0'){
				$('.sk_all').html('');
			}else{
				$('.sk_all').html(sk);
			}
			var kp = 0;
			$(".cur .money_all").each(function(){
				var val = $(this).val();
				kp+= +val;
			})
			if(kp=='0'){
				$('.kp_all').html('');
			}else{
				$('.kp_all').html(kp);
			}
			if(all!=''){
				if(val!=''){
					$(this).parents('tr').find("[name='bili[]']").val(end2);
					$(this).parents('tr').find('.bili').html("<i>"+end2+"</i>%");
					var sk = $('.sk_all').html(),
						ts = sk/all*100,
						last = ts.toFixed(2)-0;
					if(sk){
						$('.bili_all').html(""+last+"%");
					}
				}
			}
		})
		$("[name='contract_money']").blur(function(){
			var val = $(this).val();
			if(val==''){
				$('.bili').html('');
				$('.bili_all').html('');
				$("[name='bili[]']").val('');
			}else{
				$(".cur .money_in").each(function(){
					var all = $(this).val(),
						end = all/val*100,
						end2 = end.toFixed(2)-0;
					if(all!=''){
						$(this).parents('tr').find('.bili').html("<i>"+end2+"</i>%");
						$(this).parents('tr').find("[name='bili[]']").val(end2);
					}
				})
				var sk = $('.sk_all').html(),
					ts = sk/val*100,
					last = ts.toFixed(2)-0;
				if(sk){
					$('.bili_all').html(""+last+"%");
				}
			}

		})
		$("body").on('blur','.cur .money_all',function(){
			var kp = 0;
			$(".cur .money_all").each(function(){
				var val = $(this).val();
				kp+= +val;
			})
			$('.kp_all').html(kp);
		})
		/*表单验证*/
		$(".btn-submit").click(function(){
			var tag = false;
			
			$('tr.cur').each(function(){
				var kpval = ($(this).find("[name='ticket_date[]']").val().replace(/-/,'')).replace(/-/,''),
					yjval = ($(this).find("[name='warn_date[]']").val().replace(/-/,'')).replace(/-/,''),
					ckval = ($(this).find("[name='press_date[]']").val().replace(/-/,'')).replace(/-/,'');
				if(yjval>ckval){
					$(this).find("[name='press_date[]']").addClass('red_border');
					$('.bili_item').html("预警日期不能大于催款日期").show();
					tag = true;
				}
				if(kpval>yjval){
					$(this).find("[name='warn_date[]']").addClass('red_border');
					$('.bili_item').html("应开票日期不能大于预警日期").show();
					tag = true;
				}
			})
			
			$("[name='money']").each(function(){
				var val = $(this).val();
				if(val!='' && !/^[1-9]\d*$/.test(val)){
					$(this).nextAll('.error-item').eq(1).show();
					tag = true;
				}
			})
			$("[name='contract_money']").each(function(){
				var val = $(this).val();
				if(val!='' && !/^[0-9]+$/.test(val)){
					$(this).nextAll('.error-item').eq(1).show();
					tag = true;
				}
			})
			
			var kp = 0,
				moneyall = $("[name='contract_money']").val(),
				tuse = true;
			$(".cur .money_all").each(function(){
				var val = $(this).val();
				kp+= +val;
				if(val!='' && !/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(val)){
					$(this).nextAll('.error-item').eq(1).show();
					tag = true;
				}
				if(val==''){
					tuse = false;
				}
			})

			var sk = 0,
				tuee = true;
			$(".cur .money_in").each(function(){
				var val = $(this).val();
				sk+= +val;
				if(val!='' && !/^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$/.test(val)){
					$(this).nextAll('.error-item').eq(1).show();
					tag = true;
				}
				if(val==''){
					tuee = false;
				}
			})
			if(moneyall!=''){
				if(tuse == true && kp!=moneyall){
					$('.bili_item').html("应开票金额合计应等于合同金额").show();
					tag = true;
				}
				if(sk!=moneyall && tuee == true){
					$('.bili_item').html("应收款金额合计应等于合同金额").show();
					tag = true;
				}
			}else{
				if(kp!=sk){
					$('.bili_item').html("应开票金额合计应等于应收款金额合计").show();
					tag = true;
				}
			}
			
			$('.cur .td_text').each(function(){
				var val = $(this).val();
				if(val==''){
					$(this).next('.error-item').show();
					tag = true;
				}
			})
			$('.required_text').each(function(){
				var val = $(this).val();
				if(val==''){
					$(this).next('.error-item').show();
					tag = true;
				}
			})
			$('.required_select').each(function(){
				var val = $(this).val();
				if(val==-10){
					$(this).siblings('.error-item').show();
					tag = true;
				}
			})
			$(".file-name").each(function(){
				$val = $(this).val();
				if(($val=="" || /^\s+$/.test($val)) && !$(this).prop("readonly")){
					$(this).parents(".input-group").nextAll(".error-item").eq(1).show();
					tag = true;
				}
			});
			if(tag){
				return false;
			}
		});

		/*取消错误提示*/
		$('.required_select,.td_text').change(function(){
			$(this).siblings('.error-item').hide();
		})
		$(".required_text,.td_text,[name='contract_money']").focus(function(){
			$(this).nextAll('.error-item').hide();
		})
		$('.money_in').blur(function(){
			var val = $(this).val();
			if(val!=''){
				$('.money_all').siblings('.error-item').hide();
			}
		})
		$('.money_all,.money_in').focus(function(){
			$('.bili_item').hide();
		})
		$("[name='press_date[]'],[name='warn_date[]'],[name='ticket_date[]']").click(function(){
			$(this).removeClass('red_border');
			$('.bili_item').hide();
		})
		$("[name='ticket_date[]']").click(function(){
			$(this).parents('tr').find("[name='press_date[]']").removeClass('red_border');
			$(this).parents('tr').find("[name='warn_date[]']").removeClass('red_border');
		})
	});
	function dd(){
		var val = $(this).val(),
			chuo = new Date(val).getTime(),
			th = chuo+15*24*60*60*1000,
			tr = chuo+30*24*60*60*1000,
			las = gettime(th),
			lat = gettime(tr);
		$(this).parents('tr').find("[name='warn_date[]']").val(las).siblings('.error-item').hide();
		$(this).parents('tr').find("[name='press_date[]']").val(lat).siblings('.error-item').hide();
	}
	function gettime(t){
		var _time=new Date(t);
			year=_time.getFullYear();
		    month=_time.getMonth()+1;
		    date=_time.getDate();
		if(month<10 && date<10){
			return   year+"-0"+month+"-0"+date;
		}else if(month<10 && date>=10){
			return   year+"-0"+month+"-"+date;
		}else if(month>=10 && date<10){
			return   year+"-"+month+"-0"+date;
		}else if(month>=10 && date>=10){
			return   year+"-"+month+"-"+date;
		}
		
	}
</script>
</html>